package com.test.hongbao;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;

/**
 * Created by chenlong on 16/2/5.
 */
public class HongbaoPreference {

    private static final String PREFERENCES_NAME = "HongbaoPreference";

    private static HongbaoPreference preferencesUtil;
    private SharedPreferences sharedPreferences;


    public static HongbaoPreference getInstance(Context context) {
        if (preferencesUtil == null) {
            preferencesUtil = new HongbaoPreference(context);
        }
        return preferencesUtil;
    }

    private HongbaoPreference(Context context) {
        sharedPreferences = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
    }

    private void save(SharedPreferences.Editor editor) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            editor.apply();
        } else {
            editor.commit();
        }
    }

    public void setAutoHongbao(boolean isHongbaoOpen) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("isHongbaoOpen", isHongbaoOpen);
        save(editor);
    }

    public boolean isAutoHongbao() {
        return sharedPreferences.getBoolean("isHongbaoOpen", true);
    }


}
